package cn.bellychang.leetcode.question125;

/**
 * @author ChangLiang
 * @date 2020/10/26
 */
public class Solution {

    public boolean isPalindrome(String s) {
        // 首先将非字母和数字字符去掉 包括空格
        s = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();

        String first;
        String second;
        String reverseSecond;
        // 找到字符串的中点
        int middleIndex;
        if (s.length() % 2 == 0) {
            middleIndex = s.length() / 2;
            first = s.substring(0, middleIndex);
            second = s.substring(middleIndex);
        } else {
            middleIndex = s.length() / 2;
            first = s.substring(0, middleIndex);
            second = s.substring(middleIndex+1);
        }
        // 做翻转
        reverseSecond = new StringBuilder(second).reverse().toString();
        return first.equals(reverseSecond);
    }
}
